package com.yuncheng.spcyApi.gxca;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.util.encoders.Base64;

import java.io.*;
import java.util.Scanner;

/**
 * 鑾峰彇鏂囦欢SM3 HASH 鎽樿
 * @author Administrator
 *
 */
public class GetFileSM3Helper {


	 /**
     * 鑾峰彇鏂囦欢鐨凷M3鎽樿
     * @param filePath  鏂囦欢璺緞
     * @return
     */
    public String getFileSM3Hash(String filePath) {
    	File file = new File(filePath);
		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
	    BufferedInputStream bin = null;
	    try {
            bin = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[1024];
            while (bin.read(buffer) > 0) {
                bos.write(buffer);
            }
            byte[] hash = hash(bos.toByteArray());
            //return Base64.encodeBase64String(hash).replaceAll("\r\n", "");
            return Base64.toBase64String(hash).replaceAll("\r\n", "");
        }catch(Exception e) {
        	e.printStackTrace();
        	return null;
        }finally {
        	try {
				bos.close();
				bin.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }


    /**
     * 璁＄畻SM3鎽樿鍊�
     *
     * @param srcData 鍘熸枃
     * @return 鎽樿鍊硷紝32瀛楄妭
     */
    public static byte[] hash(byte[] srcData) {
        SM3Digest digest = new SM3Digest();
        digest.update(srcData, 0, srcData.length);
        byte[] hash = new byte[digest.getDigestSize()];
        digest.doFinal(hash, 0);
        return hash;
    }

    public static void main(String args[]) {


        //设置文件路径
        Scanner scanner=new Scanner(System.in);
        System.out.println("1:base64格式转pdf       2:文件sm3算法       3:json排序 ");
        System.out.println("输入要执行的程序：");
        int input=scanner.nextInt();
        System.out.println("执行的程序是："+input);
        switch (input){
            case 1:
                //对文件进行sm3算法
                //输入当前文件的路径
                System.out.println("请输入当前文件的路径：(D://Desktop/药监局文件/test.ofd)");
                String fileurl2=scanner.next();
                String testFilePath=fileurl2;
                GetFileSM3Helper getSM3=new GetFileSM3Helper();
                String fileSM3Hash = getSM3.getFileSM3Hash(testFilePath);
                System.out.println("SM3加密算法开始.......");
                System.out.print("SM3加密结果为:"+fileSM3Hash);
                return;
            //2:json排序
            case 2:
                String jsonStr="{\"fileNo\":\"test2.ofd\",\"appKey\":\"gxyjj\",\"nonce\":\"nonce1234\",\"timeStamp\":\"1669963545126\",\"signature\":\"5752997973ef166e2ebf4d90ac4a5673\",\"sealName\":\"测试印章\",\"fileType\":\"ofd\",\"source\":\"1\",\"fileData\":\"http://114.115.181.136:9232/test.ofd\",\"sm3Data\":\"VQMVWaEirrYCXF9MTRhXt/tvkVD2R9zpVLpnOQxr3cc=\",\"ruleInfo\":\"AUTO_ADD:0,-1,0,0,255,测试)|(4,\",\"retureType\":\"1\",\"certSn\":\"2200000000000019cfd1\"}";
                JSONObject jsonObject=JSON.parseObject(jsonStr);
                System.out.println(JSONSortHelper.getSortJson(jsonObject));
                return;
        }
    }
}
